Implement Repeating Key XOR

http://cryptopals.com/sets/1/challenges/5


In [ ]:
plaintext = """Burning 'em, if you ain't quick and nimble
I go crazy when I hear a cymbal""";

In [ ]:
key="ICE";

In [ ]:
function stringToBytes(X::String)
    return [UInt8(X[ii]) for ii in 1:length(X)]
end;

In [ ]:
function repeatToLength(a::Array{UInt8,1}, len::Int64)
    if len < length(a)
        return a[1:len]
    else
        b = repeat(a, outer=convert(Int64, ceil(len / length(a))))
        return b[1:len]
    end
end;

In [ ]:
function repeatingKeyXOR(pt::Array{UInt8,1}, key::Array{UInt8,1})
    return pt $ repeatToLength(key, length(pt))
end;

In [ ]:
function repeatingKeyXOR(pt::String, key::String)
    return repeatingKeyXOR(stringToBytes(pt), stringToBytes(key))
end;

In [ ]:
cipherbytes = repeatingKeyXOR(plaintext, key);

In [ ]:
bytes2hex(cipherbytes)

In [ ]: